<?php
    print_r($_GET);
    //获取get传参
    $cate_id = $_GET['id'];
    $cate_path = $_GET['path'];
    $cate_pid = $_GET['pid'];
    //查询该分类下的所有子类id
    $sql = "select id,cname,path from ".PRE."category where path like '".$cate_path.$cate_id.",%'";
    $cate_list = query($link, $sql);

    //查询该分类的父类,如果父类的id不为0的话,就继续查
    $top_name = [];
    $pid = $cate_pid;
    $i = 0;
    $sql = "select cname from ".PRE."category where id = '$cate_id' limit 1";
    $now_cname = getRow($link,$sql);
    $top_name[0]=$now_cname['cname'];
    while($pid != 0){
        //查看父类的名称
        $sql = "select cname,pid from ".PRE."category where id = '$pid' limit 1";
        $res = getRow($link,$sql);
        $pname = $res['cname'];
        $pid = $res['pid'];
        $i = $i + 1;
        $top_name[$i] = $pname;
    }



    //查询左侧栏的目录分类
    list($left_0,$left_1) = explode(',',$cate_path);
    if(empty($left_1)){
        //当点击的是顶级分类的时候,他的父类id就是0,
        //而自增id是从0开始的,就给他赋值为,该顶级分类的自身id
        if($cate_pid==0){
            $cate_pid = $cate_id;
        }
        $left_1 = $cate_pid;
    }
    //查询左侧栏顶级分类id
    $sql = "select cname from ".PRE."category where id = $left_1";
    $top_cate_name = getRow($link, $sql);
    $top_cate_name = $top_cate_name['cname'];
    //重新拼接$cate_path
    $cate_path = $left_0.','.$left_1.',';
    $sql = "select id,pid,cname,path,concat(path,id,',') as bpath from ".PRE."category where path like '".$cate_path."%' order by
bpath";
    $cate_list_left = query($link, $sql);
//    echo "<pre>";
//    print_r($cate_list_left)



?>

<div class="container">
    <!--左侧栏-->
    <div class="container mt50 col-md-2">

        <?php if(!empty($cate_list_left)):?>
            <ul>
                <li>
                    <!--顶级分类-->
                    <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;"?>
                    <a href="<?php echo ROOT_URL?>category_goods.php?id=<?php echo $left_1?>&path=<?php echo $left_0.',';?>&pid=0">
                        <?php echo $top_cate_name?>
                    </a>
                </li>
                <?php foreach($cate_list_left as $item):?>
                    <li>
                        <!--分类前的空格数-->
                        <?php echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",substr_count($item['path'],","))?>
                        <a href="<?php echo ROOT_URL?>category_goods.php?id=<?php echo $item['id']?>&path=<?php echo $item['path']?>&pid=<?php echo $item['pid']?>">
                            <?php echo $item['cname']?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif;?>
    </div>
    <!--主要内容右边栏-->
    <div class="container mt50 col-md-10">
        <div class="row mt20">
            <div class="row col-md-12">
                <h3>
                    <?php
                        for($j = count($top_name)-1; $j >= 0; $j--){
                            echo $top_name[$j].'>';
                        }
                        echo "<hr>";
                    ?>
                </h3>
            </div>
            <!--如果该分类下没有子分类,就查找该分类下的商品-->
            <?php if(empty($cate_list)):?>
                <?php
                    $sql = "
                            select
                            i.iname,g.gname,g.price,g.msg,g.id,c.cname,c.pid
                            from
                            ".PRE."goods as g,".PRE."image as i,".PRE."category as c
                            where
                            g.id = i.goods_id and c.id = g.cate_id and g.is_hot = 1 and g.state = 1 and i.cover =1 and g.cate_id = '".$cate_id."'";
                    $goods_info = query($link, $sql);

                ?>
                <!--如果该分类下有商品-->
                <?php if(!empty($goods_info)):?>
                    <?php foreach ( $goods_info as $item):?>
                        <div class="col-sm-6 col-md-3 mt20">
                            <div class="thumbnail">
                                <!-- 商品图片-->
                                <a href="./goods_details.php?id=<?php echo $item['id']?>">
                                    <img src="<?php echo get_img_url(ROOT_URL . './upload',$item['iname'],350)?>">
                                </a>
                                <div class="caption">
                                    <!-- 商品名字-->
                                    <a href="./goods_details.php?id=<?php echo $item['id']?>">
                                        <h3><?php echo $item['gname']?></h3>
                                    </a>
                                    <!-- 商品介绍-->
                                    <p style="overflow: hidden;height: 60px"><?php echo $item['msg'];?></p>
                                    <!-- 商品价格-->
                                    <h3>
                                        <span class="glyphicon glyphicon-jpy"></span>
                                        <?php echo $item['price']?>

                                        <a href="./com/buydo.php?act=zan&goods_id=<?php echo $item['id']?>" class="pull-right text-danger">
                                            <span class="glyphicon glyphicon-heart"></span>
                                        </a>


                                    </h3>
                                </div>
                            </div>
                        </div>
                    <?php endforeach;?>
                <?php else:?>
                    <div class="row col-md-12">
                        <h2 class="text-center text-danger">暂无数据</h2>
                    </div>
                <?php endif;?>

            <?php else:?>
                <!--如果该分类下有子分类,-->
                <?php foreach ( $cate_list as $value):?>
                    <!--查看该分类下是否有子分类-->
                    <?php
                        //拼接path
                        $path = $value['path'].$value['id'].',';
                        $sql = "select id from ".PRE."category where path like '$path%' limit 1";
                        $resu = getRow($link, $sql);
                    ?>
                        <!--如果给分类下没有子分类,就把该分类的名字显示出来-->
                        <?php if(empty($resu)):?>
                            <div class="row col-md-12">
                                <h2 class="text-center">
                                    --<?php echo $value['cname']?>--
                                </h2>
                            </div>
                        <?php endif;?>

                    <?php
                    //查询每一种商品的详细信息根据它所属的种类
                    $sql = "
                        select
                        i.iname,g.gname,g.price,g.msg,g.id,c.cname
                        from
                        ".PRE."goods as g,".PRE."image as i,".PRE."category as c
                        where
                        g.id = i.goods_id and c.id = g.cate_id and g.is_hot = 1 and g.state = 1 and i.cover =
                        1 and g.cate_id = '".$value['id']."'";
                    $goods_info = query($link, $sql);
                    ?>
                    <!--如果该分类下有商品才继续输出-->
                    <?php if(!empty($goods_info)):?>

                        <?php foreach ( $goods_info as $item):?>
                            <div class="col-sm-6 col-md-3 mt20">
                                <div class="thumbnail">
                                    <!-- 商品图片-->
                                    <a href="./goods_details.php?id=<?php echo $item['id']?>">
                                        <img src="<?php echo get_img_url(ROOT_URL . './upload',$item['iname'],350)?>">
                                    </a>
                                    <div class="caption">
                                        <!-- 商品名字-->
                                        <a href="./goods_details.php?id=<?php echo $item['id']?>">
                                            <h3><?php echo $item['gname']?></h3>
                                        </a>
                                        <!-- 商品介绍-->
                                        <p style="overflow: hidden;height: 60px"><?php echo $item['msg'];?></p>
                                        <!-- 商品价格-->
                                        <h3>
                                            <span class="glyphicon glyphicon-jpy"></span>
                                            <?php echo $item['price']?>

                                            <a href="./com/buydo.php?act=zan&goods_id=<?php echo $item['id']?>" class="pull-right text-danger">
                                                <span class="glyphicon glyphicon-heart"></span>
                                            </a>


                                        </h3>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach;?>
                    <?php else:?>
                        <?php if(empty($resu)):?>
                            <div class="row col-md-12">
                                <h3 class="text-center text-danger">
                                    暂无数据
                                </h3>
                            </div>
                        <?php endif;?>
                    <?php endif;?>
                <?php endforeach;?>
            <?php endif;?>

        </div>
    </div>
</div>

